﻿using System;

namespace Xww.Text.Process {
    [Serializable]
    public class RegexReplacer1in3LineProcessor : LineProcessorBase {
        MatchType matchType;
        public override MatchType MatchType {
            get { return matchType; }
        }

        public override bool TrueMatch(string input) {
            return regexReplacer.REGEX.Match(input).Success;
        }

        public override string TrueProcess(string input) {
            return regexReplacer.REGEX.Replace(input, OutputPattern);
        }

        RegexReplacer regexReplacer;
        public RegexReplacer1in3LineProcessor(string inputPattern, string outputPattern,MatchType type, bool ignoreCase) {
            InputPattern = inputPattern;
            OutputPattern = outputPattern;
            matchType = type;
            IgnoreCase = ignoreCase;

            regexReplacer = RegexHelper.Create(inputPattern, outputPattern, type, !ignoreCase, true, false);
        }

        #region Static Creator

        public static IProcessLine Create1in3Replacer(string input, string output, MatchType type, bool ignoreCase, bool ifProceed) {
            RegexReplacer1in3LineProcessor rst = new RegexReplacer1in3LineProcessor(input, output, type, ignoreCase);
            rst.IfProceed = ifProceed;
            rst.InverseMatch = rst.MatchOnly = false;
            return rst;
        }
        public static IProcessLine Create1in3IgnoreReplacer(string input, MatchType type, bool ignoreCase, bool ifProceed, bool inverseMatch) {
            RegexReplacer1in3LineProcessor rst = new RegexReplacer1in3LineProcessor(input, "", type, ignoreCase);
            rst.IfProceed = ifProceed;
            rst.InverseMatch = inverseMatch;
            rst.MatchOnly = true;
            return rst;
        }

        #endregion

    }
}
